package Database;

import java.sql.*;
import java.util.*;

public class Database {
	private static final String driverDB = "com.mysql.jdbc.Driver";
	private static final String urlDB = "jdbc:mysql://localhost:3306/";
	private static final String nameDB = "http_server";
	private static final String userDB = "root";
	private static final String passwordDB = "";
	
	private Connection sqlConnection;
	
	public Database() throws SQLException, ClassNotFoundException {
		this.loadDriver(driverDB);
		this.sqlConnection = getConnection(urlDB, nameDB, userDB, passwordDB);
	}
	
	//Sukonstruoja duombazes objekta
	public Database(String driverDB, String urlDB, String nameDB, String userDB, String passDB) throws SQLException, ClassNotFoundException {
		this.loadDriver(driverDB);
		sqlConnection = this.getConnection(urlDB, nameDB, userDB, passDB);
	}
	
	private void loadDriver(String driver) throws ClassNotFoundException {
        Class.forName(driver);
	}

	private Connection getConnection(String urlDB, String nameDB, String userDB, String passwordDB) throws SQLException {
        Connection sqlConnection = null;
        sqlConnection = DriverManager.getConnection(urlDB + nameDB, userDB, passwordDB);

        return sqlConnection ;
    }
	
	//atsijungimui nuo DB
	public void closeConnection() throws SQLException {
        if (null != this.sqlConnection) {
            this.sqlConnection.close();
        }
	}
	
	//Ivykdo sql uzklauso mysql duombazeje be rezultato.
	public void query(String sqlQuery) throws SQLException {
        Statement statement = sqlConnection.createStatement();

        statement.execute(sqlQuery);
	}
	
	//Ivykdo sql uzklausa ir grazina pirma rezultato eilute. (Lengviau dirbt, kai norim tik vienos eilutes ir grazins ne masyva)
	public Row queryRow(String sqlQuery) throws SQLException {
        Row row = new Row();

        Statement statement = sqlConnection.createStatement();
        ResultSet result = statement.executeQuery(sqlQuery);

        if (result.next()) {
            ResultSetMetaData rsmd = result.getMetaData();
            int columnCount = rsmd.getColumnCount();

            // The column count starts from 1
            for (int i = 1; i < columnCount + 1; i++ ) {
                String columnName = rsmd.getColumnName(i);
                String rowBoxValue = result.getString(columnName);

                row.insertValue(columnName, rowBoxValue);
            }
        } else {
            row = null;
        }
    
        return row;
	}
	
	//Ivykdo sql uzklausa ir grazina rezultatu eiluciu masyva. (Jei selectas ne vienai eilutei)
	public Row[] queryRows(String sqlQuery) throws SQLException {
        List<Row> rows = new ArrayList<Row>();

        Statement statement = sqlConnection.createStatement();
        ResultSet result = statement.executeQuery(sqlQuery);

        ResultSetMetaData rsmd = result.getMetaData();
        int columnCount = rsmd.getColumnCount();

        while (result.next()) {
            Row rowTmp = new Row();
            for (int i = 1; i < columnCount + 1; i++ ) {
                String columnName = rsmd.getColumnName(i);
                String rowBoxValue = result.getString(columnName);

                rowTmp.insertValue(columnName, rowBoxValue);
            }
            rows.add(rowTmp);
        }

        Row[] rowsArray = new Row[rows.size()];
        return rows.toArray(rowsArray);
	}
}